﻿using System;
using System.Collections.Generic;

namespace PortableTPL
{
    public static class Parallel
    {
        public static void For(int inclusiveStart, int exclusiveEnd, ParallelOptions parallelOptions, Action<int> action)
        {
            InternalFor(inclusiveStart, exclusiveEnd, action, parallelOptions);
        }

        public static void For(int inclusiveStart, int exclusiveEnd, Action<int> action)
        {
            InternalFor(inclusiveStart, exclusiveEnd, action);
        }

        public static void ForEach<T>(IList<T> list, ParallelOptions parallelOptions, Action<T> action)
        {
            InternalForEach(list, action, parallelOptions);
        }

        public static void ForEach<T>(IList<T> list, Action<T> action)
        {
            InternalForEach(list, action);
        }

        static void InternalFor(int inclusiveStart, int exclusiveEnd, Action<int> action, ParallelOptions parallelOptions = null)
        {
            using (StandardTaskScheduler localScheduler = new StandardTaskScheduler())
            {
                localScheduler.Dispatch(inclusiveStart, exclusiveEnd, action, parallelOptions);
            }
        }

        static void InternalForEach<T>(IList<T> list, Action<T> action, ParallelOptions parallelOptions = null)
        {
            using (StandardTaskScheduler localScheduler = new StandardTaskScheduler())
            {
                localScheduler.Dispatch(list, action, parallelOptions);
            }
        }
    }
}
